package com.dorso.controller;

import com.dorso.entity.Supplier;
import com.dorso.exception.ProductAdminException;
import com.dorso.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id){
        Supplier result = supplierService.getById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }
    @PostMapping("")
    public Supplier addProduct(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.saveOrUpdate(supplier);
        return supplier;
    }
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable long id){
        supplierService.removeById(id);
    }

    @PutMapping("/{id}")
    public void updateProduct(@PathVariable long id,@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.saveOrUpdate(supplier);
    }
}
